package com.zretc.support.controller;

import com.zretc.support.entity.Answer;
import com.zretc.support.entity.ApiResult;
import com.zretc.support.entity.Score;
import com.zretc.support.service.AnswerService;
import com.zretc.support.service.ScoreService;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.ApiResultHandler;
import com.zretc.support.tool.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api")
public class AnswerController {
    @Autowired
    AnswerService answerService;
    @Autowired
    ScoreService scoreService;

    /**
     * 根据examId获得Answer信息
     *
     * @param examId
     * @return
     */
    @RequestMapping("answerId/{examId}")
    public ApiResult getAnswer(@PathVariable("examId") long examId) {

        Answer res = answerService.getByExamId(examId);
        if (res == null) {
            return ApiResultHandler.buildApiResult(10000, "考试编号不存在", null);
        }
        return ApiResultHandler.buildApiResult(200, "请求成功！", res);
    }

    /**
     * 分页查询->
     *
     * @param page
     * @param size
     * @param keywords
     * @return
     */
    @RequestMapping("/answer/page")
    public PageData<Answer> getByPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "limit", defaultValue = "5") int size,
                                      @RequestParam(value = "keywords", defaultValue = "") String keywords
    ) {
        if (page == 0) {
            page = 1;
        }
        Page<Answer> list = answerService.getByPage(keywords, (page - 1), size);
        PageData<Answer> pageData = new PageData<>();
        // 获得数据总数
        pageData.setRecords(list.getContent());
        //当前页数
        pageData.setSize(list.getSize());

        //共几条数据
        pageData.setTotal((int) list.getTotalElements());
        pageData.setCurrent(list.getNumber());
        // 还有: code 从layui哪里来的
        pageData.setMsg("访问成功！");
        return pageData;
    }

    @RequestMapping(value = "/questionapi/UpdateAnswer")
    public AjaxResponse UpdateAnswerScore(@RequestParam("answerId") int answerId,
                                          @RequestParam("score") double score) {

        AjaxResponse ajaxResponse= new AjaxResponse("修改失败！", 0);
        int flag = answerService.updSocre(score, answerId);
        if (flag == 1) {
            // 根据answerId 获取 -> scoreId
            Answer answer = answerService.getByAnswerId(answerId);
            Score score1 = scoreService.getScoresByExamIdAndUerId(answer.getExamId(),answer.getUserId());
            scoreService.updScore(answerId,score,answer.getExamId(),answer.getUserId(),score1.getScoreId());
            ajaxResponse = new AjaxResponse("修改成功！", 200);
        }

        return ajaxResponse;
    }

}
